<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="company-box box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="role">
            <!-- 添加按钮 -->
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addRole">新增角色</el-button>
            <!-- 表格 -->
            <el-table
              border
              :data="roles"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                label="序号"
                width="150"
              />
              <el-table-column
                prop="name"
                label="角色名"
                width="150"
                sortable
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                prop="address"
                label="操作"
                width="250"
                align="center"
              >
                <template v-slot="{row}">
                  <el-button size="mini" type="text" @click="setPermissionFn(row)">分配权限</el-button>
                  <el-button size="mini" type="text" @click="editRole(row.id)">修改</el-button>
                  <el-button size="mini" type="text" @click="delRoleFn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="page.page"
              :page-size="page.pagesize"
              layout="total, prev, pager, next"
              :total="total"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="info">
            <el-form ref="form" label-width="200px" disabled>
              <el-form-item label="企业名称">
                <el-input v-model="CompanyInfo.name" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="CompanyInfo.companyAddress" />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="CompanyInfo.companyPhone" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="CompanyInfo.mailbox" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="CompanyInfo.remarks" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 添加角色对话框 -->
    <el-dialog :title="title" :visible.sync="editRoleDialog" @closed="closeDialog">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="角色名称" label-width="90px" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色描述" label-width="90px" prop="description">
          <el-input v-model="form.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editRoleDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateRole">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分配权限对话框 -->
    <el-dialog
      :title="permissionTitle"
      :visible.sync="isShowSetPermissionDialog"
      width="50%"
      @close="onClose"
    >
      <el-tree
        v-if="isShowSetPermissionDialog"
        ref="tree"
        :data="permissionList"
        show-checkbox
        default-expand-all
        :props="defalutProp"
        check-strictly
        node-key="id"
        :default-checked-keys="TheRolepermissionList"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose()">取 消</el-button>
        <el-button type="primary" @click="saveUpdatePermission">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { GetAllRoleListAPI, DelRoleByIdAPI, GetTheRoleByIdAPI, EditTheRoleByIdAPI, AddTheRoleByIdAPI, GetCompanyInfoAPI } from '@/api/setting'
import { getAllPermissionAPI } from '@/api/permission'
import { getTreeList } from '@/utils'
import { getTheRoleListAPI, setTheRoleListAPI } from '@/api/role'
export default {
  name: 'Settings',
  data() {
    return {
      activeName: 'role', // 空时tabs栏选中  值是一栏的name
      roles: [], // 表格数据
      page: {
        page: 1,
        pagesize: 10
      }, // 请求信息
      total: 10, // 总条数
      editRoleDialog: false, // 编辑对话框的显示
      form: {
        name: '',
        description: ''
      }, // 弹框表单内容
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }, // 弹窗校验规则
      CompanyInfo: {},
      isShowSetPermissionDialog: false,
      permissionTitle: '',
      defalutProp: {
        label: 'name'
      },
      permissionList: [],
      TheRolepermissionList: [],
      updateRoleData: {}
    }
  },
  computed: {
    title() {
      return (this.form.id ? '编辑' : '新增') + '角色'
    }
  },

  async created() {
    this.GetAllRoleList() // 获取角色列表
    this.CompanyInfo = await GetCompanyInfoAPI(1)
  },

  methods: {
    // 获取角色列表
    async GetAllRoleList() {
      const res = await GetAllRoleListAPI(this.page)
      this.roles = res.rows
      this.total = res.total
    },
    // 页码值发生变化
    handleCurrentChange(value) {
      this.page.page = value
      this.GetAllRoleList()
    },
    // 删除角色
    delRoleFn(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await DelRoleByIdAPI(id)
        this.$message.success('删除成功!')
        this.GetAllRoleList()
      }).catch(() => {})
    },
    // 编辑角色
    async editRole(id) {
      const res = await GetTheRoleByIdAPI(id)
      this.editRoleDialog = true
      this.form = res
      this.form.description = res.description
    },
    // 点击确认修改 / 添加角色
    async updateRole() {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          await EditTheRoleByIdAPI(this.form)
        } else {
          await AddTheRoleByIdAPI(this.form)
        }
        this.editRoleDialog = false
        this.$message.success('修改成功')
        this.GetAllRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 监听关闭弹框事件
    closeDialog() {
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 新增角色
    addRole() {
      this.editRoleDialog = true
    },
    // 分配权限 显示对话框
    async setPermissionFn(row) {
      this.permissionTitle = `为${row.name}分配权限`
      this.isShowSetPermissionDialog = true
      const res = await getAllPermissionAPI()
      this.updateRoleData = await getTheRoleListAPI(row.id)
      this.TheRolepermissionList = this.updateRoleData.permIds
      this.permissionList = getTreeList('0', res)
    },
    // 点击确定保存权限
    async  saveUpdatePermission() {
      const arr = this.$refs.tree.getCheckedKeys()
      if (!arr.length) return this.$message.error('请至少分配一个权限')
      this.updateRoleData.permIds = arr
      await setTheRoleListAPI(this.updateRoleData)
      this.$message.success('分配权限成功')
      this.onClose()
      this.GetAllRoleList()
    },
    // 对话框关闭
    onClose() {
      this.isShowSetPermissionDialog = false
      this.TheRolepermissionList = []
    }
  }
}
</script>

<style scoped lang='scss'>
.company-box {

    ::v-deep.el-tabs__item {
      font-size: 20px;
      width: 150px;
      text-align: center;
   }
   .el-pagination {
     text-align: right;
   }
   .el-input {
     width: 350px;
   }
   .el-form {
     padding: 20px;
     ::v-deep label {
       font-weight: 400;
     }
   }
  //  .el-dialog {
  //    .el-form {
  //      .el-form-item{
  //         ::v-deep .el-form-item__label {
  //           font-weight: 400;
  //         }
  //      }
  //    }
  //  }
}
</style>
